<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>登录</title>
	<style>
		* {
			font-size: 14px;
			font-family: "microsoft yahei";
		}
		body {
			background-color: #393D49;
		}
		canvas{
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            z-index: -1;
        }
		div.login {
			width: 250px;
			background: rgba(180, 180, 180, .8);
			margin: 0 auto;
			margin-top: 15%;
			padding: 20px;
			border-radius: 10px;
		}
		div.login input, div.login button {
			border: none;
			display: block;
            padding: 10px;
            outline: none;
		}
		div.login input {
			width: 230px;
			margin-bottom: 1em;
            background: rgba(255, 255, 255, .85);
		}
		div.login button {
			width: 250px;
			margin-top: 1em;
            background: #5FB878;
            color: #FFF;
            font-size: 15px;
		}
		div.login input, div.login button, div.login img {
            border-radius: 3px;
        }
        div.login img {
            width: 250px;
            height: 40px;
        }
	</style>
</head>
<body>
	<div class="login">
		<input type="text" id="user" placeholder="帐号" value="admin">
		<input type="password" id="pass" placeholder="密码" value="123456">
		<input type="text" id="captcha" placeholder="验证码" value="<?php echo $this->session->userdata('captcha'); ?>">
		<img src="<?php echo $captcha; ?>" alt="">
		<button type="button">登 录</button>
	</div>
	<canvas></canvas>
</body>
</html>
<script src="<?php echo base_url('static/layui-v2.3.0/layui.js'); ?>"></script>
<script>
layui.use(["jquery", "layer"], function() {
    var $ = layui.jquery, layer = layui.layer;

    $('[type="button"]').on("click", function() {
        var $this = $(this);
        if ("正在登录..." == $this.html()) {
            return ;
        }
        $user = $('[id="user"]');
        if ($user.val() == '') {
            layer.msg('请输入帐号', {icon: 5, anim: 6}, function() {
                $user.focus();
            });
            return false;
        }

        $pass = $('[id="pass"]');
        if ($pass.val() == '') {
            layer.msg('请输入密码', {icon: 5, anim: 6}, function() {
                $pass.focus();
            });
            return false;
        }

        $captcha = $('[id="captcha"]');
        if ($captcha.val() == '') {
            layer.msg('请输入验证码', {icon: 5, anim: 6}, function() {
                $captcha.focus();
            });
            return false;
        }

        $this.html("正在登录...");
        $.post("<?php echo site_url('admin/user/login'); ?>?"+ Math.random(), {
            "user": $user.val(),
            "pass": $pass.val(),
            "captcha": $captcha.val(),
        }, function(data) {
            $("img").attr("src", "<?php echo site_url('admin/user/captcha'); ?>?"+ Math.random());
            layer.msg(data.message, {
                icon: "1" == data.status ? 6 :5,
                anim: 6
            }, function() {
                if ("1" == data.status) {
                    window.location.href = data.href;
                }
                $this.html("登 录");
            });
        }, "json");
        return ;
    });
    $('img').click(function(e) {
        $.get("<?php echo site_url('admin/user/captcha'); ?>?"+ Math.random(), function(res) {
            console.log(res);
            $("img").attr("src", res);
        });
    });
});
window.onload = function() {
    var canvas = document.querySelector('canvas'),
        ctx = canvas.getContext('2d');
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;

    ctx.lineWidth = 0.3;
    ctx.strokeStyle = (new Color(150)).style;
    var mousePosition = {
        x: 30 * canvas.width / 100,
        y: 30 * canvas.height / 100
    };
    var dots = {
        nb: 750,
        distance: 50,
        d_radius: 100,
        array: []
    };
    function colorValue(min) {
        return Math.floor(Math.random() * 255 + min);
    }
    function createColorStyle(r,g,b) {
        return 'rgba(' + r + ',' + g + ',' + b + ', 0.8)';
    }
    function mixComponents(comp1, weight1, comp2, weight2) {
        return (comp1 * weight1 + comp2 * weight2) / (weight1 + weight2);
    }
    function averageColorStyles(dot1, dot2) {
        var color1 = dot1.color,
            color2 = dot2.color;

        var r = mixComponents(color1.r, dot1.radius, color2.r, dot2.radius),
            g = mixComponents(color1.g, dot1.radius, color2.g, dot2.radius),
            b = mixComponents(color1.b, dot1.radius, color2.b, dot2.radius);
        return createColorStyle(Math.floor(r), Math.floor(g), Math.floor(b));
    }

    function Color(min) {
        min = min || 0;
        this.r = colorValue(min);
        this.g = colorValue(min);
        this.b = colorValue(min);
        this.style = createColorStyle(this.r, this.g, this.b);
    }

    function Dot(){
        this.x = Math.random() * canvas.width;
        this.y = Math.random() * canvas.height;

        this.vx = -.5 + Math.random();
        this.vy = -.5 + Math.random();

        this.radius = Math.random() * 2;
        this.color = new Color();
    }

    Dot.prototype = {
        draw: function(){
            ctx.beginPath();
            ctx.fillStyle = this.color.style;
            ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
            ctx.fill();
        }
    };

    function createDots(){
        for(i = 0; i < dots.nb; i++){
            dots.array.push(new Dot());
        }
    }

    function moveDots() {
        for(i = 0; i < dots.nb; i++){
            var dot = dots.array[i];
            if(dot.y < 0 || dot.y > canvas.height){
                dot.vx = dot.vx;
                dot.vy = - dot.vy;
            }
            else if(dot.x < 0 || dot.x > canvas.width){
                dot.vx = - dot.vx;
                dot.vy = dot.vy;
            }
            dot.x += dot.vx;
            dot.y += dot.vy;
        }
    }

    function connectDots(){
        for(i = 0; i < dots.nb; i++){
            for(j = i; j < dots.nb; j++){
                i_dot = dots.array[i];
                j_dot = dots.array[j];
                if((i_dot.x - j_dot.x) < dots.distance && (i_dot.y - j_dot.y) < dots.distance && (i_dot.x - j_dot.x) > - dots.distance && (i_dot.y - j_dot.y) > - dots.distance){
                    if((i_dot.x - mousePosition.x) < dots.d_radius && (i_dot.y - mousePosition.y) < dots.d_radius && (i_dot.x - mousePosition.x) > - dots.d_radius && (i_dot.y - mousePosition.y) > - dots.d_radius){
                        ctx.beginPath();
                        ctx.strokeStyle = averageColorStyles(i_dot, j_dot);
                        ctx.moveTo(i_dot.x, i_dot.y);
                        ctx.lineTo(j_dot.x, j_dot.y);
                        ctx.stroke();
                        ctx.closePath();
                    }
                }
            }
        }
    }

    function drawDots() {
        for(i = 0; i < dots.nb; i++){
            var dot = dots.array[i];
            dot.draw();
        }
    }

    function animateDots() {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        moveDots();
        connectDots();
        drawDots();
        requestAnimationFrame(animateDots);
    }
    document.querySelector('canvas').addEventListener('mousemove',function(e){
        mousePosition.x = e.pageX;
        mousePosition.y = e.pageY;
    })

    document.querySelector('canvas').addEventListener('mouseleave',function(e){
    	mousePosition.x = canvas.width / 2;
        mousePosition.y = canvas.height / 2;
    })

    createDots();
    requestAnimationFrame(animateDots);
}
</script>